<?php 
if($_GET['act']=='keywords'){//设置关键字
	$tagId = empty($_GET['editid']) ? 0 : intval($_GET['editid']);
	
	//获取标签名称
	$tagInfo = array();
	if($tagId>0){
		$sql="SELECT `id`,`tags` FROM ".TAG." WHERE `id` = '{$tagId}' LIMIT 1";
		$db->query($sql);
		$result=$db->getRows();
		if(!empty($result)){
			$tagInfo = $result[0];
		}
	}
	if(empty($tagInfo)){
		exit('标签id错误');
	}
	
	//保存关键字
	if(!empty($_POST)){
		$name = @mysql_escape_string(trim($_POST['name']));
		$url = @mysql_escape_string(trim($_POST['url']));
		$displayorder = intval($_POST['displayorder']);
		$is_display = intval($_POST['is_display']);
		$is_highlight = intval($_POST['is_highlight']);
		$kid = intval($_POST['kid']);
		if($kid<=0){
			//新增
			//是否存在
			$sql="SELECT `id` FROM `haiou_tag_floor_keywords` WHERE `name` = '{$name}' AND `tag_id`='{$tagId}' LIMIT 1";
			$db->query($sql);
			$result=$db->getRows();
			if(!empty($result)){
				exit(json_encode(array('code'=>'-1','msg'=>'此关键词已存在')));
			}
			$dateTime = date('Y-m-d H:i:s');
			$sqlstr = "insert into `haiou_tag_floor_keywords` (`tag_id`,`name`,`url`,`displayorder`, `is_display`,`is_highlight`,`update_time`) values ('{$tagId}','{$name}','{$url}', '{$displayorder}','{$is_display}','{$is_highlight}','{$dateTime}')";
			$result = $db->query($sqlstr);
			if($result){
				exit(json_encode(array('code'=>'0','msg'=>'成功')));
			}else{
				exit(json_encode(array('code'=>'-1','msg'=>'服务器异常,请重试')));
			}
		}else{
			//修改
			$sqlstr = "UPDATE `haiou_tag_floor_keywords` SET `name`='{$name}',`url`='{$url}',`displayorder`='{$displayorder}', `is_display`='{$is_display}',`is_highlight`='{$is_highlight}' WHERE `id`={$kid}";
			$result = $db->query($sqlstr);
			if($result){
				exit(json_encode(array('code'=>'0','msg'=>'成功')));
			}else{
				exit(json_encode(array('code'=>'-1','msg'=>'服务器异常,请重试')));
			}
		}
		exit;
	}
	
	
	/* 列表 */
	//获取已有关键词
	$sql="SELECT * FROM `haiou_tag_floor_keywords` WHERE `tag_id` = '{$tagId}' ORDER BY IF(`is_display`>0,1,0) DESC,`displayorder` ASC";
	$db->query($sql);
	$keywords=$db->getRows();
	foreach($keywords as $k=>$v){
		if($v['is_highlight'] != '0'){
			$keywords[$k]['is_highlight_text'] = '<font color="green">是</font>';
		}else{
			$keywords[$k]['is_highlight_text'] = '否';
		}
		if($v['is_display'] != '0'){
			$keywords[$k]['is_display_text'] = '是';
		}else{
			$keywords[$k]['is_display_text'] = '<font color="red">否</font>';
		}
	}
	
	$tpl->assign('keywords',json_encode(array('data'=>$keywords)));
	$tpl->assign('tagInfo',$tagInfo);
	$tpl->display('product_tags_floor_keywords.htm');
}elseif($_GET['act']=='recpro'){//设置推荐商品
	$product_type=array('index_best');
	if(!empty($_POST['submit'])&&$_POST["submit"]==lang_show('submit'))
	{
		unset($_POST['submit']);
		foreach ($product_type as $item){
			if (!empty($_POST[$item.'_id'])){
				$sql="update ".PROTAGSRECPRO ." set status='-1' where tag_id = '{$_POST['tag_id']}'";
				$db->query($sql);
				foreach($_POST[$item.'_id'] as $info_key=> $info_value)
				{
					//如果已经存在就更新
					$sql = "select id from ".PROTAGSRECPRO." where tag_id='{$_POST['tag_id']}' and product_id='{$info_value}'";
					$db->query($sql);
					$recproid = $db->fetchField('id');
					if($recproid>0){
						$sql="update ".PROTAGSRECPRO ." set product_title='{$_POST[$item.'_title'][$info_key]}',displayorder='{$_POST[$item.'_displayorder'][$info_key]}',status='1' where id = '{$recproid}'";
						$db->query($sql);
					}else{
						$sql1="insert into ".PROTAGSRECPRO." (`tag_id`,`product_id`,product_title,`displayorder`,`create_user`) values ('{$_POST['tag_id']}','{$info_value}','{$_POST[$item.'_title'][$info_key]}','{$_POST[$item.'_displayorder'][$info_key]}','$_SESSION[ADMIN_USER]')";
						$db->query($sql1);
					}
				}
			}
		}
		admin_msg("module.php?m=product&s=product_tags.php","设置成功");
		exit;
	}elseif (!empty($_GET['info_ids'])&&in_array($_GET['type'],$product_type)){
		$sql="select a.id,a.pic,a.price,a.pname,b.product_title,b.displayorder,b.status,b.tag_id from ".PRO." as a left join ".PROTAGSRECPRO." as b on a.id=b.product_id  where a.id in ({$_GET['info_ids']}) order by b.displayorder ASC,FIND_IN_SET(a.id,'{$_GET['info_ids']}') ASC";
		$db->query($sql);
		$re=$db->getRows();
		foreach ($re as $val)
		{
			if(empty($res[$val['id']])){
				$res[$val['id']] = $val;
			}else{
				if($val['tag_id']>0 && $val['tag_id']==$_GET['tag_id'] && $val['status']>0){
					$res[$val['id']] = $val;
				}
			}
		}
		echo json_encode(array_values($res));
		exit;
	}
	
	$sql = "select GROUP_CONCAT(product_id  ORDER BY displayorder) as info_ids FROM ".PROTAGSRECPRO." where tag_id='{$_GET['editid']}' and status>0";
	$db->query($sql);
	$re=$db->fetchRow();
	
	$tpl->assign('tag_name',$_GET['tag_name']);
	$tpl->assign('info_ids',$re['info_ids']);
	$tpl->display('product_tags_floor_recpro.htm');
}